package com.woniu.outlet.dao;

import com.woniu.outlet.po.ApplicationDetail;
import com.woniu.outlet.po.MaterialApplication;
import com.woniu.outlet.po.MaterialApplicationExample;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

@Repository
public interface MaterialApplicationMapper {
    long countByExample(MaterialApplicationExample example);

    int deleteByExample(MaterialApplicationExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(MaterialApplication record);

    int insertSelective(MaterialApplication record);

    List<MaterialApplication> selectByExample(MaterialApplicationExample example);

    MaterialApplication selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") MaterialApplication record, @Param("example") MaterialApplicationExample example);

    int updateByExample(@Param("record") MaterialApplication record, @Param("example") MaterialApplicationExample example);

    int updateByPrimaryKeySelective(MaterialApplication record);

    int updateByPrimaryKey(MaterialApplication record);

    @Select("<script>" +
            "select m.production_plan_id productionPlanId,w.username,d.name deptName,m.application_time applicationTime\n" +
            "from ERP_material_application m,erp_system_management.ERP_worker w, erp_system_management.ERP_dept d \n" +
            "where m.dept_id = d.id and m.user_id = w.id\n" +
            "<if test='t1 !=null and t1 != \"\"'>and m.application_time &gt;  #{t1}</if>" +
            "<if test='t2 !=null and t2 != \"\"'>and m.application_time &lt;  #{t2}</if>" +
            "</script>")
    List<MaterialApplication> selectAll (@Param("t1") Date t1, @Param("t2") Date t2);


    @Select("select app.id,app.application_id applicationId,app.material_code materialCode,app.material_name materialName,app.specification_model specificationModel,app.specification_unit specificationUnit,app.valuation_unit valuationUnit,app.application_num applicationNum,app.spare2\n" +
            "from ERP_application_detail app,ERP_material_application mat \n" +
            "where app.application_id = mat.id and mat.application_code = #{applicationCode}")
    List<ApplicationDetail> selectMaterialByCode (String applicationCode);

    @Update("update ERP_material_application set spare1 = \"已领取\" where application_code = #{s} ")
    void receive (String s);

    @Select("select mat.*\n" +
            "from ERP_material_application mat\n" +
            "where mat.application_code = #{applicationCode} and spare1 = \"未领取\"")
    MaterialApplication selectApp (String applicationCode);

    @Update("update ERP_material_application set spare1 = \"已备齐\" where application_code = #{s}")
    void ready (String s);

    int insertMat (MaterialApplication materialApplication);
}